package com.ruoyi.collegeClass.service.impl;

import java.util.*;

import com.ruoyi.collegeClass.domain.BwStudent;
import com.ruoyi.collegeClass.domain.vo.CollegeTeacherVo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.domain.SysDept;

import com.ruoyi.system.api.domain.SysUser;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ruoyi.collegeClass.mapper.BwClassMapper;
import com.ruoyi.collegeClass.domain.BwClass;
import com.ruoyi.collegeClass.service.IBwClassService;

/**
 * 班级Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-24
 */
@Service
public class BwClassServiceImpl implements IBwClassService 
{
    @Autowired
    private BwClassMapper bwClassMapper;

    /**
     * 查询班级
     * 
     * @param id 班级主键
     * @return 班级
     */
    @Override
    public BwClass selectBwClassById(Long id)
    {
        return bwClassMapper.selectBwClassById(id);
    }

    /**
     * 查询班级列表
     * 
     * @param bwClass 班级
     * @return 班级
     */
    @Override
    public List<BwClass> selectBwClassList(BwClass bwClass)
    {
        return bwClassMapper.selectBwClassList(bwClass);
    }

    /**
     * 新增班级
     * 
     * @param bwClass 班级
     * @return 结果
     */
    @Override
    public int insertBwClass(BwClass bwClass)
    {

        List<BwClass> bwClass1 = bwClassMapper.selectBwTeacher(bwClass.getManageId());
        for (BwClass aClass : bwClass1) {
            if (aClass!=null && aClass.getIsFinish()!=1){


                return 0;
            }
        }



        Random rand = new Random();
        int random = rand.nextInt((999999 - 100000)+ 1) + 100000;
        bwClass.setPassword(String.valueOf(random));
        bwClass.setCreateTime(DateUtils.getNowDate());
        return bwClassMapper.insertBwClass(bwClass);
    }

    /**
     * 修改班级
     * 
     * @param bwClass 班级
     * @return 结果
     */
    @Override
    public int updateBwClass(BwClass bwClass)
    {
        bwClass.setUpdateTime(DateUtils.getNowDate());
        Integer isFinish = bwClass.getIsFinish();
        if (isFinish!=null && isFinish ==1){
            bwClassMapper.updateStudent(bwClass.getId());
        }
        return bwClassMapper.updateBwClass(bwClass);
    }

    /**
     * 批量删除班级
     * 
     * @param ids 需要删除的班级主键
     * @return 结果
     */
    @Override
    public int deleteBwClassByIds(Long[] ids)
    {
        return bwClassMapper.deleteBwClassByIds(ids);
    }

    /**
     * 删除班级信息
     * 
     * @param id 班级主键
     * @return 结果
     */
    @Override
    public int deleteBwClassById(Long id)
    {
        return bwClassMapper.deleteBwClassById(id);
    }

    @Override
    public SysDept selectDeptById(Long id) {
        return bwClassMapper.selectDeptById(id);
    }

    @Override
    public SysUser selectUserById(Long id) {
        return bwClassMapper.selectUserById(id);
    }

    @Override
    public R collegeTeacher() {
        List<CollegeTeacherVo> collegeTeacherVos = new ArrayList<>();
        List<SysDept> sysDeptList =  bwClassMapper.selectDept();

        for (SysDept sysDept : sysDeptList) {
            CollegeTeacherVo collegeTeacherVo = new CollegeTeacherVo(sysDept.getDeptId(), sysDept.getDeptName());

            List<BwClass> bwClasses = bwClassMapper.selectBwClassByDeptId(sysDept.getDeptId());
            for (BwClass bwClass : bwClasses) {
                if (bwClass.getIsFinish() ==1){
                    CollegeTeacherVo collegeTeacherVo1 = new CollegeTeacherVo(bwClass.getId(), bwClass.getClassName()+("-(毕业班)"));



                 List<SysUser> sysUserList = bwClassMapper.selectUserByClassId(bwClass.getManageId());

                for (SysUser sysUser : sysUserList) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("value",sysUser.getUserId());
                    map.put("label",sysUser.getNickName());

                    collegeTeacherVo1.addChildren(map);
                }
                collegeTeacherVo.addChildren(collegeTeacherVo1);
            }    else {
                    CollegeTeacherVo collegeTeacherVo1 = new CollegeTeacherVo(bwClass.getId(), bwClass.getClassName());



                    List<SysUser> sysUserList = bwClassMapper.selectUserByClassId(bwClass.getManageId());

                    for (SysUser sysUser : sysUserList) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("value",sysUser.getUserId());
                        map.put("label",sysUser.getNickName());

                        collegeTeacherVo1.addChildren(map);
                    }
                    collegeTeacherVo.addChildren(collegeTeacherVo1);
                }
            }

            collegeTeacherVos.add(collegeTeacherVo);
        }
        return R.ok(collegeTeacherVos);
    }

    @Override
    public R collegeDirection() {

        List<CollegeTeacherVo> collegeTeacherVos = new ArrayList<>();
        List<SysDept> sysDeptList =  bwClassMapper.selectDept();

        for (SysDept sysDept : sysDeptList) {
            CollegeTeacherVo collegeTeacherVo = new CollegeTeacherVo(sysDept.getDeptId(), sysDept.getDeptName());

            List<BwClass> bwClasses = bwClassMapper.selectBwClassByDeptId(sysDept.getDeptId());
            for (BwClass bwClass : bwClasses) {
                CollegeTeacherVo collegeTeacherVo1 = new CollegeTeacherVo(bwClass.getId(), bwClass.getClassName());

                List<SysUser> sysUserList = bwClassMapper.selectUserByClass(bwClass.getGuidanceId());

                for (SysUser sysUser : sysUserList) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("value",sysUser.getUserId());
                    map.put("label",sysUser.getNickName());

                    collegeTeacherVo1.addChildren(map);
                }
                collegeTeacherVo.addChildren(collegeTeacherVo1);
            }

            collegeTeacherVos.add(collegeTeacherVo);
        }
        return R.ok(collegeTeacherVos);
    }

    @Override
    public R findDeptInfoByPid(Long pid) {
        List<SysDept> list = bwClassMapper.findDeptInfoByPid(pid);
        return R.ok(list);
    }


    @Scheduled(cron = "59 59 23 L * ?")
    public void getPassword(){
        List<BwClass> bwClasses = bwClassMapper.selectBwClassList(new BwClass());
        for (BwClass bwClass : bwClasses) {
            Random rand = new Random();
            int randomNumber = rand.nextInt((999999 - 100000)+ 1) + 100000; // 生成1到100之间的随机数
            bwClass.setPassword(String.valueOf(randomNumber));
            bwClassMapper.updateBwClass(bwClass);
        }

        List<BwStudent> bwStudents = bwClassMapper.selectBwStudent();
        if (!bwStudents.isEmpty()){
            for (BwStudent bwStudent : bwStudents) {
                bwClassMapper.updateStudentIsJob(bwStudent.getId());
            }
        }

    }




}
